<?php
include('conect.php');

//connection String
$con = mysql_connect($hostname, $username, $password) or die('Could not connect: ' . mysql_error());
//select database
mysql_select_db($database, $con);
mysql_query("SET NAMES utf8");  

if ($_GET['storeTip'] === 'addRow') {
    if ($_GET['acction'] === 'addrow') {}
	$denum = $_GET['denum'];
  	$subpro = $_GET['subpro'];
  	$cild = $_GET['cild'];
  	$data = $_GET['data'];
  	$suma = $_GET['suma'];
	//if(!get_magic_quotes_gpc()) {
    //	$italiacls = addslashes($italiacls);
    //    $italia = addslashes($italia);
    //    $romanacls = addslashes($romanacls);
    //    $romana = addslashes($romana);
    //}/
    $sql2 = "SELECT tip FROM subpro WHERE id='$subpro'";
	$t = mysql_query($sql2);
	$tt;
	while($obj = mysql_fetch_object($t)) {
		$tt = $obj;
	}
	$sql3 = "SELECT it FROM tip WHERE id='$tt->tip'";
    	$t3 = mysql_query($sql3);
    	$tt3;
    	while($obj3 = mysql_fetch_object($t3)) {
    		$tt3 = $obj3;
    	}
    $sql = "INSERT INTO date (denum,subpro,cild,data,suma,pro,tip,user) VALUES ('$denum','$subpro','$cild','$data','$suma',(SELECT pro FROM subpro WHERE id=$subpro),'$tt->tip','".$_GET['UserID']."')";
	$a = mysql_query($sql);
	if($a== 1){
        $sql1 = "SELECT MAX(LAST_INSERT_ID(id)) AS id FROM date";
        $b = mysql_query($sql1);
        while($obj = mysql_fetch_object($b)) {
            $info[] = array('id' => 'Info','task'  => 'S-a efectuat cu succes.', 'it'=>$obj->id, 'tip'=>$tt3->it);
        }
	}
	if($a== 1){
		$arr = array('success' => true,'rows'  => $info, $sql3);
	}else{
		$info[] = array('id' => 'Info','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);
	}
}

if ($_GET['storeTip'] === 'updateRow') {
    if ($_GET['acction'] === 'updaterow') {}
	$denum = $_GET['denum'];
  	$subpro = $_GET['subpro'];
  	$cild = $_GET['cild'];
  	$data = $_GET['data'];
  	$suma = $_GET['suma'];
  	$id = $_GET['id'];
	//if(!get_magic_quotes_gpc()) {
    //	$d = addslashes($d);
    //   $da = addslashes($da);
    //    $italia = addslashes($italia);
    //    $romana = addslashes($romana);
    //}

    $sql2 = "SELECT tip FROM subpro WHERE id='$subpro'";
	$t = mysql_query($sql2);
	$tt;
	while($obj = mysql_fetch_object($t)) {
		$tt = $obj;
	}
	$a = mysql_query("UPDATE date SET denum='$denum',subpro='$subpro',cild='$cild',data='$data',suma='$suma',tip='$tt->tip' WHERE id='$id'");
	if($a== 1){
		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.','tip'=>$tt->tip);
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'delRow') {
    if ($_GET['acction'] === 'delrow') {}
	$a =mysql_query("DELETE FROM date WHERE id='".$_GET['id']."'");
	if($a== 1){
		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'comboSubPro') {
    if ($_GET['acction'] === 'combosubpro') {}
	$rs = mysql_query("SELECT id, task, tip FROM subpro WHERE user='".$_GET['UserID']."'");
	$info = array();
	while($obj = mysql_fetch_object($rs)) {
		$info[] = $obj;
	}
	$arr = array('success' => true,'rows'  => $info);
}

if ($_GET['storeTip'] === 'comboChild') {
    if ($_GET['acction'] === 'combocild') {}
	$rs=mysql_query("SELECT id, task FROM cild WHERE user='".$_GET['UserID']."'");
	$info = array();
	while($obj = mysql_fetch_object($rs)) {
		$info[] = $obj;
	}
	$arr = array('success' => true,'rows'  => $info);
}

if ($_GET['storeTip'] === 'comboChildd') {
    if ($_GET['acction'] === 'combocildd') {}
	$subpro = $_GET['subpro'];
	$rs=mysql_query("SELECT id, task FROM cild WHERE subpro='$subpro' AND user='".$_GET['UserID']."'");
	$info = array();
	while($obj = mysql_fetch_object($rs)) {
		$info[] = $obj;
	}
	$arr = array('success' => true,'rows'  => $info);
}

if ($_GET['storeTip'] === 'comboPro') {
    if ($_GET['acction'] === 'combopro') {}
	$rs=mysql_query("SELECT id, task FROM pro WHERE user='".$_GET['UserID']."'");
	$info = array();
	while($obj = mysql_fetch_object($rs)) {
		$info[] = $obj;
	}
	$arr = array('success' => true,'rows'  => $info);
}

if ($_GET['storeTip'] === 'editChild') {
    if ($_GET['acction'] === 'editChild') {}
	$a = mysql_query("UPDATE cild SET task='".$_GET['task']."' WHERE id='".$_GET['id']."'");
	if($a== 1){
		$info[] = array('id' => 'Info','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'editSubPro') {
    if ($_GET['acction'] === 'editsubpro') {}
	$a = mysql_query("UPDATE subpro SET task='".$_GET['task']."' WHERE id='".$_GET['id']."'");
	if($a== 1){
		$info[] = array('id' => 'Info','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'editPro') {
    if ($_GET['acction'] === 'editpro') { }
	$a = mysql_query("UPDATE pro SET task='".$_GET['task']."' WHERE id='".$_GET['id']."'");
	if($a== 1){
		$info[] = array('id' => 'Info ','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info ','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'addPro') {
    if ($_GET['acction'] === 'addpro') {}
	mysql_query("INSERT INTO pro (task,user) VALUES ('".$_GET['pro']."','".$_GET['UserID']."')");
	mysql_query("INSERT INTO subpro (task,pro,tip,user) VALUES ('".$_GET['subpro']."',(SELECT MAX(LAST_INSERT_ID(id)) FROM pro),'".$_GET['tip']."','".$_GET['UserID']."')");
	$a = mysql_query("INSERT INTO cild (task, subpro,user) VALUES ('".$_GET['cild']."',(SELECT MAX(LAST_INSERT_ID(id)) FROM subpro),'".$_GET['UserID']."')");
	if($a== 1){
		$info[] = array('id' => 'Info','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'addSubPro') {
    if ($_GET['acction'] === 'addsubpro') {}
	mysql_query("INSERT INTO subpro (task,pro,tip,user) VALUES ('".$_GET['subpro']."','".$_GET['pro']."','".$_GET['tip']."','".$_GET['UserID']."')");
	$a = mysql_query("INSERT INTO cild (task, subpro,user) VALUES ('".$_GET['cild']."',(SELECT MAX(LAST_INSERT_ID(id)) FROM subpro),'".$_GET['UserID']."')");
	if($a== 1){
		$info[] = array('id' => 'Info','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'addChild') {
    if ($_GET['acction'] === 'addcild') {}
	$a = mysql_query("INSERT INTO cild (task,subpro,user) VALUES ('".$_GET['cild']."','".$_GET['subpro']."','".$_GET['UserID']."')");
	if($a== 1){
		$info[] = array('id' => 'Info','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'delPro') {
    if ($_GET['acction'] === 'delpro') {}
	$a = mysql_query("DELETE FROM pro WHERE id='".$_GET['id']."'");
	// TODO !!! Sa fac ca sa sterg tot ce este in acest proiect
	if($a== 1){
		$info[] = array('id' => 'Info','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'delSubPro') {
    if ($_GET['acction'] === 'delsubpro') {}
	$query="DELETE FROM subpro WHERE id='".$_GET['id']."'";
	mysql_query($query);
	$query1="DELETE FROM cild WHERE subpro='".$_GET['id']."'";
	$a = mysql_query($query1);
	// TODO For delete all rows from date which correspond with subpro deleted DELETE FROM date WHERE subpro = 6;
	if($a== 1){
		$info[] = array('id' => 'Info','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'delChild') {
    if ($_GET['acction'] === 'delcild') { }
	$query = "DELETE FROM cild WHERE id='".$_GET['id']."'";
	$a = mysql_query($query);
	if($a== 1){
		$info[] = array('id' => 'Info','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

//if ($_GET['acction'] === detailout) {
//	$rs=mysql_query("SELECT ( SELECT FORMAT(SUM(suma),1) FROM date WHERE tip=1 ) AS c, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip=2 ) AS v, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip=3 ) AS z, (SELECT FORMAT((SUM(suma)*10)/100,1) FROM date WHERE tip=2) AS zt, ((SELECT FORMAT(SUM(suma),1) FROM date WHERE tip=2)-(SELECT FORMAT(SUM(suma),1) FROM date WHERE tip=1)-(SELECT FORMAT(SUM(suma),1) FROM date WHERE tip=3)) AS r");
//	$info = array();
//	while($obj = mysql_fetch_object($rs)) {
//		$info[] = $obj;
//	}
//	$arr = array('success' => true,'rows'  => $info);
//}

if ($_GET['storeTip'] === 'addSumInit') {
    if ($_GET['acction'] === 'addSumInit') {}
	$a = mysql_query("INSERT INTO start (data,suma,user) VALUES ('".$_GET['data']."','".$_GET['suma']."','".$_GET['UserID']."')");
	if($a== 1){
		$info[] = array('id' => 'Info','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'getSumInit') {
    if ($_GET['acction'] === 'getSumInit') {}
	$rs = mysql_query("SELECT SUM(suma) AS sumInit FROM start WHERE user='".$_GET['UserID']."'");
	$info = array();
	while($obj = mysql_fetch_object($rs)) {
		$info[] = $obj;
	}
	$arr = array('success' => true,'rows'  => $info);
}

if ($_GET['storeTip'] === 'detail') {
    if($_GET['acction'] === 'dey'){
        $period = "AND DATE(data)=DATE(NOW())";
    } else if($_GET['acction'] === 'year'){
        $period = "AND YEAR(data)=YEAR(CURRENT_DATE)";
    } else if($_GET['acction'] === 'week'){
        $w = date("W");
        $period = "AND WEEKOFYEAR(data)=$w";
    } else if($_GET['acction'] === 'luni'){
        $period = "AND MONTH(data)='".$_GET['luna']."'";
    } else if($_GET['acction'] === 'perioada'){
        $from = $_GET['from'];
        $to = $_GET['to'];
        $period = "AND data BETWEEN '$from' AND '$to'";
    }
        $u = $_GET['UserID'];
        $sql="SELECT (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) $period AND user=$u) AS c, ".
        "(SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) $period AND user=$u) AS v, ".
        "(SELECT FORMAT(IF(SUM(suma) IS NOT NULL,SUM(suma),0),1) FROM date WHERE tip=(SELECT id FROM tip WHERE cr=3 AND user=$u) $period AND user=$u) AS z, ".
        "(SELECT FORMAT((SUM(suma)*10)/100,1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) $period AND user=$u) AS zt, ".
        "FORMAT((SELECT IF(SUM(suma) IS NOT NULL,SUM(suma),0) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) $period AND user=$u)-(SELECT IF(SUM(suma) IS NOT NULL,SUM(suma),0) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) $period AND user=$u),1) AS r, ".
        "FORMAT((SELECT IF(SUM(suma) IS NOT NULL,SUM(suma),0) FROM start WHERE user=$u)+(SELECT IF(SUM(suma) IS NOT NULL,SUM(suma),0) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND user=$u)-(SELECT IF(SUM(suma) IS NOT NULL,SUM(suma),0) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND user=$u),1) AS s";
        $rs=mysql_query($sql);
        $info = array();
        while($obj = mysql_fetch_object($rs)) {
            $info[] = $obj;
        }
	$arr = array('success' => true,'rows'  => $info, $sql);
}

if ($_GET['storeTip'] === 'graphic') {
    if ($_GET['acction'] === 'grafic') {}
    $u = $_GET['UserID'];
	$query = "SELECT ('Ian') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=1 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=1 AND user=$u) AS Venituri UNION ALL SELECT ('Feb') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=2 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=2 AND user=$u) AS Venituri UNION SELECT ('Mar') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=3 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=3 AND user=$u) AS Venituri UNION SELECT ('Apr') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=4 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=4 AND user=$u) AS Venituri UNION SELECT ('Mai') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=5 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=5 AND user=$u) AS Venituri UNION SELECT ('Iun') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=6 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=6 AND user=$u) AS Venituri UNION SELECT ('Iul') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=7 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=7 AND user=$u) AS Venituri UNION SELECT ('Aug') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=8 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=8 AND user=$u) AS Venituri UNION SELECT ('Sep') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=9 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=9 AND user=$u) AS Venituri UNION SELECT ('Oct') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=10 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=10 AND user=$u) AS Venituri UNION SELECT ('Noi') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=11 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=11 AND user=$u) AS Venituri UNION SELECT ('Dec') AS lun, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=0) AND MONTH(data)=12 AND user=$u) AS Cheltuieli, (SELECT FORMAT(SUM(suma),1) FROM date WHERE tip IN (SELECT id FROM tip WHERE sens=1) AND MONTH(data)=12 AND user=$u) AS Venituri";
	$rs=mysql_query($query);
	$info = array();
	while($obj = mysql_fetch_object($rs)) {
		$info[] = $obj;
	}
	$arr = array('success' => true,'rows'  => $info, $query);
}

if ($_GET['storeTip'] === 'detailGraphic') {
    $u = $_GET['UserID'];
    $month = array("","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
    $year = array("","2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023");
	$query = "";
    if ($_GET['acction'] === 'child') {
        $tip = "cild = ".$_GET['child']."";
        if($_GET['period'] === 'month'){
            for ($i=1; $i<=31; $i++){
                $query .= "SELECT ('".$i."') AS day";
                for($j=1; $j<=12; $j++){
                $query .= ", (SELECT FORMAT(SUM(suma),1) FROM date WHERE $tip AND DAY(data)=$i AND MONTH(data)=$j AND user=$u) AS `$month[$j]`";
                }
                if($i===1){
                    $query .= " UNION ALL ";
                } else {
                    if($i===31){

                    }else{
                        $query .= " UNION ";
                    }
                }
            }
        } else {
            for ($i=1; $i<=12; $i++){
                $query .= "SELECT ('".$month[$i]."') AS day";
                for($j=1; $j<=12; $j++){
                    $query .= ", (SELECT FORMAT(SUM(suma),1) FROM date WHERE $tip AND MONTH(data)=$i AND YEAR(data)=$year[$j] AND user=$u) AS `$year[$j]`";
                }
                if($i===1){
                    $query .= " UNION ALL ";
                } else {
                    if($i===12){

                    }else{
                        $query .= " UNION ";
                    }
                }
            }
        }
    } else if($_GET['acction'] === 'subPro'){
        $tip = "subpro = ".$_GET['subPro']."";
        if($_GET['period'] === 'month'){
            for ($i=1; $i<=31; $i++){
                $query .= "SELECT ('".$i."') AS day";
                for($j=1; $j<=12; $j++){
                $query .= ", (SELECT FORMAT(SUM(suma),1) FROM date WHERE $tip AND DAY(data)=$i AND MONTH(data)=$j AND user=$u) AS `$month[$j]`";
                }
                if($i===1){
                    $query .= " UNION ALL ";
                } else {
                    if($i===31){

                    }else{
                        $query .= " UNION ";
                    }
                }
            }
        } else {
            for ($i=1; $i<=12; $i++){
                $query .= "SELECT ('".$month[$i]."') AS day";
                for($j=1; $j<=12; $j++){
                    $query .= ", (SELECT FORMAT(SUM(suma),1) FROM date WHERE $tip AND MONTH(data)=$i AND YEAR(data)=$year[$j] AND user=$u) AS `$year[$j]`";
                }
                if($i===1){
                    $query .= " UNION ALL ";
                } else {
                    if($i===12){

                    }else{
                        $query .= " UNION ";
                    }
                }
            }
        }
    }

	$rs = mysql_query($query);
	$info = array();
	while($obj = mysql_fetch_assoc($rs)) {
		$info[] = $obj;
	}
//	var_dump($info);
	$arr = array('success' => true,'rows'  => $info, $query);
}

if ($_GET['storeTip'] === 'budgetGrid') {
    $u = $_GET['UserID'];
    $month = array("","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
    $year = array("","2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023");
    $querySubPro = "SELECT id, task FROM subpro WHERE user =$u";
    $rso = mysql_query($querySubPro);
    $subpro = array();
    while($obo = mysql_fetch_assoc($rso)) {
        array_push($subpro,  $obo);
    }
//    print_r($subpro);
	$query = "";
    if ($_GET['acction'] === 'budgetGrid') {
        if($_GET['period'] === 'year'){
            for ($i = 0; $i < count($subpro); $i++){
                $queryChild = "SELECT id, task FROM cild WHERE subpro =(SELECT id FROM subpro WHERE task='".$subpro[$i]['task']."' AND user=$u) AND user=$u";
                $rss = mysql_query($queryChild);
                $inf = array();
                while($ob = mysql_fetch_assoc($rss)) {
                    array_push($inf,  $ob);
                }
//                print_r($inf);
//                echo(count($subpro));
//                echo(count($inf));
                for($k = 0; $k < count($inf); $k++){
                    $query .= "SELECT ('".$subpro[$i]['task']."') AS subpro, ('".$subpro[$i]['id']."') AS idsubpro, ('".$inf[$k]['task']."') AS cild, ('".$inf[$k]['id']."') AS idcild";
                    for($j = 1; $j <= 12; $j++){
                        $query .= ", (SELECT FORMAT(SUM(suma),1) FROM date WHERE cild ='".$inf[$k]['id']."' AND MONTH(data)=$j AND user=$u) AS `$month[$j]`";
                    }
                    $query .= ", (SELECT FORMAT(SUM(suma),1) FROM date WHERE cild ='".$inf[$k]['id']."' AND YEAR(data)=YEAR(CURRENT_DATE()) AND user=$u) AS Total";
                    if($k===count($inf)-1) {

                    } else {
                        if($k===count($inf)){

                        }else{
                            $query .= " UNION ";
                        }
                    }
                }
                if($i > 0){
                    if($i===count($subpro)-1){ //1 e obligatoriu pentru a nu pune la urma

                    }else {
                        $query .= " UNION ";
                    }
                } else {
                     $query .= " UNION ALL ";
                }
            }
        }
    }

	$rs = mysql_query($query);
	$info = array();
	while($obj = mysql_fetch_assoc($rs)) {
		$info[] = $obj;
	}
//	var_dump($info);
	$arr = array('success' => true,'rows'  => $info, $query);
}


if ($_GET['storeTip'] === 'comboTip') {
    if ($_GET['acction'] === 'combotip') {}
	$rs=mysql_query("SELECT id, task, it FROM tip WHERE user='".$_GET['UserID']."'");
	$info = array();
	while($obj = mysql_fetch_object($rs)) {
		$info[] = $obj;
	}
	$arr = array('success' => true,'rows'  => $info);
}

if ($_GET['storeTip'] === 'color') {
    if ($_GET['acction'] === 'color') {}
	$a = mysql_query("UPDATE tip SET it='".$_GET['it']."' WHERE id='".$_GET['id']."'");
	if($a== 1){
		$info[] = array('id' => 'Info ','task'  => 'S-a efectuat cu succes.');
		$arr = array('success' => true,'rows'  => $info);
	}else{
		$info[] = array('id' => 'Info ','task'  => 'Nu s-a inregistrat!!!');
		$arr = array('success' => true,'rows'  => $info);}
}

if ($_GET['storeTip'] === 'details_sum') {
    if($_GET['acction'] === 'day'){
            $period = "AND DATE(data)=DATE(NOW())";
    } else if($_GET['acction'] === 'year'){
            $period = "AND YEAR(data)=YEAR(CURRENT_DATE)";
    } else if($_GET['acction'] === 'week'){
            $w = date("W");
            $period = "AND WEEKOFYEAR(data)=$w";
    } else if($_GET['acction'] === 'luni'){
            $period = "AND MONTH(data)='".$_GET['luna']."'";
    } else if($_GET['acction'] === 'perioada'){
            $from = $_GET['from'];
            $to = $_GET['to'];
            $period = "AND data BETWEEN '$from' AND '$to'";
    }
    $query = "SELECT id FROM cild WHERE user='".$_GET['UserID']."'";
	$rs = mysql_query($query);
	$info = array();
	while($obj = mysql_fetch_object($rs)) {
        $query1 = "SELECT (SELECT task FROM cild WHERE id='".$obj->id."') as denum, (SELECT id FROM cild WHERE id='".$obj->id."') as cild, FORMAT(SUM(suma),1) as suma FROM date WHERE cild='".$obj->id."' $period AND user='".$_GET['UserID']."'";
        $rs1 = mysql_query($query1);
	    while($obj1 = mysql_fetch_object($rs1)) {
		    $info[] = $obj1;
	    }
	}
	$arr = array('success' => true,'rows'  => $info);
}

//// De aici in jos nu am testat nimic pentru MyMoney data BETWEEN '.$from.' AND '.$to.'

//
//if ($_GET['acction'] === 'verific') {
//	$d = $_GET['denum'];
//    $sql= "SELECT d.italiacls, d.romanacls, t.task AS subcateg, c.task AS categ FROM date AS d, cild AS t, subpro AS c WHERE d.cild=t.it AND t.subpro=c.id AND d.italia='$d'";
//    $rs = mysql_query($sql);
//    $grid = array();
//	while($obj = mysql_fetch_object($rs)) {
//		$grid[] = $obj;
//	}
//	$arr = array(
//		'rows'  => $grid
//	);
//}
//
//
//
//
//
//if ($_GET['acction'] === 'editpro') {
//	$a = mysql_query("UPDATE pro SET task='".$_GET['task']."' WHERE id='".$_GET['id']."'");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//
//
//if ($_GET['acction'] === 'pro') {
//	$rs=mysql_query("SELECT id, task FROM pro WHERE root='".$_GET['pro']."'");
//	$info = array();
//	while($obj = mysql_fetch_object($rs)) {
//		$info[] = $obj;
//		//$arr[] = $obj;
//	}
//	$arr = array('success' => true,'rows'  => $info);
//}
//
//
//
//if ($_GET['acction'] === 'combocateg') {
//	$rs=mysql_query("SELECT id, task FROM subpro WHERE pro='".$_GET['pro']."'");
//	$info = array();
//	while($obj = mysql_fetch_object($rs)) {
//		$info[] = $obj;
//		//$arr[] = $obj;
//	}
//	$arr = array('success' => true,'rows'  => $info);
//}
//
//
//
//if ($_GET['acction'] === 'editroot') {
//	$a = mysql_query("UPDATE root SET task='".$_GET['task']."' WHERE id='".$_GET['id']."'");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//if ($_GET['acction'] === 'addroot') {
//	$a = mysql_query("INSERT INTO root (task) VALUES ('".$_GET['task']."')");
//	$rs=mysql_query("SELECT MAX(LAST_INSERT_ID(id))AS ittt FROM root");
//	while($obj = mysql_fetch_object($rs)) {
//		$info[] = array('id' => 'Info','text' => 'S-a efectuat cu succes.', 'ittt'=> $obj->ittt );
//	}
//	if($a== 1){
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//if ($_GET['acction'] === 'delroot') {
//	$a = mysql_query("DELETE FROM root WHERE id='".$_GET['id']."'");
//	// !!! Sa fac ca sa sterg tot ce este in acest proiect
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//if ($_GET['acction'] === 'combosubcateg') {
//	$rs=mysql_query("SELECT id, text FROM tip");
//	$info = array();
//	while($obj = mysql_fetch_object($rs)) {
//		$info[] = $obj;
//	}
//	$arr = array('success' => true,'rows'  => $info);
//}
//
//if ($_GET['acction'] === 'editcateg') {
//	$a = mysql_query("UPDATE subpro SET task='".$_GET['subcateg']."' WHERE id='".$_GET['categorie']."'");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//if ($_GET['acction'] === 'editsub') {
//	$a = mysql_query("UPDATE cild SET task='".$_GET['subcateg']."' WHERE it='".$_GET['categorie']."'");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//
//
//if ($_GET['acction'] === 'delrow') {
//	$a =mysql_query("DELETE FROM date WHERE id='".$_GET['categorie']."'");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//if ($_GET['acction'] === 'movepro') {
//	$a = mysql_query("UPDATE pro SET root='".$_GET['root']."' WHERE id='".$_GET['id']."'");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//if ($_GET['acction'] === 'copypro') {
//	$a = mysql_query("INSERT INTO pro (`task`, `root`) (SELECT `task`, ".$_GET['root']." AS root FROM pro WHERE id='".$_GET['id']."')");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//if ($_GET['acction'] === 'movesubpro') {
//	//$a1 = mysql_query("UPDATE subpro SET pro='".$_GET['pro']."' WHERE id='".$_GET['id']."'");
//	$a = mysql_query("UPDATE subpro SET pro='".$_GET['pro']."' WHERE id='".$_GET['id']."'");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//if ($_GET['acction'] === 'copysubpro') {
//	$a = mysql_query("INSERT INTO subpro (`task`, `pro`) (SELECT `task`, ".$_GET['pro']." AS pro FROM subpro WHERE id='".$_GET['id']."')");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//if ($_GET['acction'] === 'movecild') {
//	//$a1 = mysql_query("UPDATE su SET pro='".$_GET['pro']."' WHERE id='".$_GET['id']."'");
//	$a = mysql_query("UPDATE cild SET subpro='".$_GET['cat']."' WHERE it='".$_GET['id']."'");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}
//
//if ($_GET['acction'] === 'copycild') {
//	$a = mysql_query("INSERT INTO cild (`task`, `subpro`) (SELECT `task`, ".$_GET['cat']." AS subpro FROM cild WHERE it='".$_GET['id']."')");
//	if($a== 1){
//		$info[] = array('id' => 'Info','text'  => 'S-a efectuat cu succes.');
//		$arr = array('success' => true,'rows'  => $info);
//	}else{
//		$info[] = array('id' => 'Info','text'  => 'Nu s-a inregistrat!!!');
//		$arr = array('success' => true,'rows'  => $info);}
//}

mysql_close($con);
echo json_encode($arr);
?>